package com.example.as_sum.util;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonUtil {
	public static String StringToJson(String[] names, Object[] values) {
		if (names.length != values.length) {
			return null;
		}

		JSONObject json = null;
		try {
			json = new JSONObject();

			for (int i = 0; i < names.length; i++) {
				json.put(names[i], values[i]);
			}

		} catch (JSONException e) {

			Log.e(e);
		}
		return json.toString();

	}
	public static JSONObject getJsonObject(Object o){
		JSONObject object=null;
		try {
			object= new JSONObject((String)o);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return  object==null?new JSONObject():object;
	}

	public static JSONArray getJsonArray(Object object,String token){
		JSONArray array=null;
		try {
			JSONObject o= new JSONObject((String)object);
			array=o.getJSONArray(token);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return  array==null?new JSONArray():array;
	}
	public static  JSONArray getJsonArray(Object object){
		return getJsonArray(object,"list");
	}
	
	
	
}
